package com.dao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.dao.UniversalDao;

@SuppressWarnings("unchecked")
@Repository
public class UniversalDaoImpl extends HibernateDaoSupport implements UniversalDao {
    
    @Autowired
    public void setHibernateSessionFaction(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    public <T> void delete(T t) {
        getHibernateTemplate().delete(t);
    }

    public <T> T get(Class<T> clazz, Serializable id) {
        return (T) getSession().get(clazz, id);
    }

   
    public <T> List<T> getAll(Class<T> clazz) {
        return getSession().createCriteria(clazz).list();
    }

    public <T> void save(T t) {
        getHibernateTemplate().save(t);
    }

    public <T> void update(T t) {
        getHibernateTemplate().update(t);
    }

    @Override
    public <T> List<T> findByHql(final String hql) {
        List list = getHibernateTemplate().executeFind(new HibernateCallback() {
            @Override
            public Object doInHibernate(org.hibernate.Session session)
                    throws HibernateException, SQLException {
                Query query = session.createQuery(hql);
                return query.list();
            }
        });
        return list;
    }

    public <T> List<T> findByDetachedCriteria(DetachedCriteria criteria) {
        return getHibernateTemplate().findByCriteria(criteria);
    }

    @Override
    public <T> void saveOrUpdateAll(Collection<T> entitys) {
        getHibernateTemplate().saveOrUpdateAll(entitys);
    }

    @Override
    public <T> void clear() {
        getHibernateTemplate().clear();
    }

    @Override
    public <T> void merge(T t) {
        getHibernateTemplate().merge(t);
    }

    @Override
    public <T> void flush() {
        getHibernateTemplate().flush();
    }

    @Override
    public <T> void evict(T t) {
        getHibernateTemplate().evict(t);
    }

    @Override
    public <T> List<T> findByHql(String hql, Object... objects) {
        return getHibernateTemplate().find(hql, objects);
    }

    @Override
    public List<HashMap<String, Object>> findToMap(final String sql) throws Exception {

        List list = getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session)
            throws HibernateException, SQLException {
                Query query = session.createSQLQuery(sql);
                return query
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
                .list();
            }
        });
        return list;

    }

    @Override
    public <T> void deleteAll(Collection<T> entitys) {
        getHibernateTemplate().deleteAll(entitys);
    }

    @Override
    public <T> List<T> findByExample(Object exampleEntity) {
        return getHibernateTemplate().findByExample(exampleEntity);
    }
    

    @Override
    public Session rrentSession() {
        Session session = (Session)getHibernateTemplate().execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                return session;
            }
        });
        return session;
    }

    @Override
    public Session currentSession1() {
        return getSession();
    }
    
    public void release(Session session) {
        releaseSession(session);
    }
    
}
